<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.lampinfo_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <!--<template slot-scope="{row}"
                slot="roadSection">
        <el-tag>{{row.roadSectionName}}</el-tag>
      </template>-->

    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/lamp/lampinfo";
  //import {getAreaTree} from "@/api/lamp/area";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 350,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
            labelWidth: 120,
            menuType:'menu',
            menuWidth: 120,
          column: [
              {
                  label: "路灯信息分组",
                  minWidth:120,
                  prop: "name",
                  rules: [{
                      required: true,
                      message: "请输入路灯信息分组名称",
                      trigger: "blur"
                  }]
              },
            {
              label: "网络协议类型",
                minWidth:110,
              prop: "netProtrolType",
              rules: [{
                required: true,
                message: "请输入网络协议类型",
                trigger: "blur"
              }]
            },
            {
              label: "协议类型",
              prop: "protrolType",
              rules: [{
                required: true,
                message: "请输入协议类型",
                trigger: "blur"
              }]
            },
              /*{
                  label: "路段[街道]",
                  prop: "roadSection",
                  multiple: true,
                  type: "tree",
                  dicData: [],
                  slot: true,
                  minWidth: 110,
                  props: {
                      label: "title",
                      value: "value"
                  },
                  rules: [{
                      required: true,
                      message: "请选择路段[街道]",
                      trigger: "click"
                  }]
              },*/
            {
              label: "灯杆高度",
              prop: "lampPoleHeight",
              rules: [{
                required: true,
                message: "请输入灯杆高度",
                trigger: "blur"
              }]
            },
            {
              label: "灯杆直径",
              prop: "lampPoleDiameter",
              rules: [{
                required: true,
                message: "请输入灯杆直径",
                trigger: "blur"
              }]
            },
            {
              label: "灯杆材质",
              prop: "lampPoleMaterial",
              rules: [{
                required: true,
                message: "请输入灯杆材质",
                trigger: "blur"
              }]
            },
            {
              label: "灯具类型",
              prop: "lampType",
              rules: [{
                required: true,
                message: "请输入灯具类型",
                trigger: "blur"
              }]
            },
            {
              label: "太阳能板类型",
                minWidth:110,
              prop: "solarPanelType",
              rules: [{
                required: true,
                message: "请输入太阳能板类型",
                trigger: "blur"
              }]
            },
            {
              label: "蓄电池类型",
                minWidth:100,
              prop: "batteryType",
              rules: [{
                required: true,
                message: "请输入蓄电池类型",
                trigger: "blur"
              }]
            },
            {
              label: "太阳能板功率",
                minWidth:110,
              prop: "solarPanelPower",
              rules: [{
                required: true,
                message: "请输入太阳能板功率",
                trigger: "blur"
              }]
            },
              {
                  label: "负载功率",
                  prop: "loadPower",
                  rules: [{
                      required: true,
                      message: "请输入负载功率",
                      trigger: "blur"
                  }]
              },
            {
              label: "蓄电池AH数",
                minWidth:100,
              prop: "batteryAh",
              rules: [{
                required: true,
                message: "请输入蓄电池AH数",
                trigger: "blur"
              }]
            },
            {
              label: "备注信息",
              prop: "remarks",
              rules: [{
                required: true,
                message: "请输入备注信息",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.lampinfo_add, false),
          viewBtn: this.vaildData(this.permission.lampinfo_view, false),
          delBtn: this.vaildData(this.permission.lampinfo_delete, false),
          editBtn: this.vaildData(this.permission.lampinfo_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, loading, done) {
          //row.roadSection = row.roadSection.join(",");
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
          //row.roadSection = row.roadSection.join(",");
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
              /*this.form.roadSection = this.form.roadSection.split(",");
              this.form.roadSection.forEach((ele, index) => {
                  this.form.roadSection[index] = Number(ele);
              });*/
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
          /*getAreaTree().then(res => {
              const index = this.$refs.crud.findColumnIndex("roadSection");
              this.option.column[index].dicData = res.data.data;
          });*/
      }
    }
  };
</script>

<style>
</style>
